package common

import (
	"example.com/m/model"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/postgres"
)

var DB *gorm.DB

func InitDB() *gorm.DB {
	//连接数据库
	//DriverName := viper.GetString("datasource.DriverName")
	//host := viper.GetString("datasource.host")
	//port := viper.GetString("datasource.port")
	//dbname := viper.GetString("datasource.dbname")
	//username := viper.GetString("datasource.username")
	//password := viper.GetString("datasource.password")
	//args := fmt.Sprintf("%s:%s %s %s %s",username,password,host,port,dbname)

	db, err := gorm.Open("postgres", "host=127.0.0.1 port=5432 user=cow11023 dbname=ginessential password=Cowzyr299025")
	//db,err := gorm.Open(DriverName,args)
	//log.Println(args)
	if err != nil {
		return nil
	}
	db.AutoMigrate(&model.User{})
	DB = db
	return db
}

func GetDB() *gorm.DB {
	//获取DB实例
	return DB
}
